#!/bin/bash

# Docker管理面板 v1.0
# 作者: 高级工程师
# 功能: Docker安装和管理

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
WHITE='\033[1;37m'
NC='\033[0m' # No Color

# 打印带颜色的消息
print_message() {
    local color=$1
    local message=$2
    echo -e "${color}${message}${NC}"
}

# 打印标题
print_title() {
    clear
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${WHITE}🐳 Docker容器管理工具 v1.0${NC}"
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo
}

# 检查Docker是否已安装
check_docker_installed() {
    if command -v docker &> /dev/null; then
        return 0
    else
        return 1
    fi
}

# 安装Docker
install_docker() {
    print_message $YELLOW "正在安装Docker最新版本..."
    
    # 更新yum源
    print_message $BLUE "更新yum源..."
    yum update -y
    
    # 安装必要的工具
    print_message $BLUE "安装必要的工具..."
    yum install -y yum-utils device-mapper-persistent-data lvm2
    
    # 尝试多种安装方式
    docker_installed=false
    
    # 方式1: 使用yum安装
    print_message $BLUE "方式1: 使用yum安装Docker..."
    
    # 添加Docker官方yum源
    yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    
    if yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin; then
        docker_installed=true
        print_message $GREEN "✅ yum安装Docker成功！"
    else
        print_message $YELLOW "⚠️  yum安装失败，尝试其他方式..."
    fi
    
    # 方式2: 如果yum失败，尝试使用官方安装脚本
    if [ "$docker_installed" = false ]; then
        print_message $BLUE "方式2: 使用官方安装脚本..."
        if curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh; then
            docker_installed=true
            print_message $GREEN "✅ 官方脚本安装Docker成功！"
        else
            print_message $YELLOW "⚠️  官方脚本安装失败，尝试最后方式..."
        fi
    fi
    
    # 方式3: 如果都失败，尝试使用包管理器直接安装
    if [ "$docker_installed" = false ]; then
        print_message $BLUE "方式3: 使用包管理器直接安装..."
        if yum install -y docker; then
            docker_installed=true
            print_message $GREEN "✅ 包管理器安装Docker成功！"
        else
            print_message $RED "❌ 所有安装方式都失败了！"
            exit 1
        fi
    fi
    
    # 检查Docker是否安装成功
    if ! command -v docker &> /dev/null; then
        print_message $RED "❌ Docker安装失败！请检查网络连接和系统配置"
        exit 1
    fi
    
    # 创建Docker服务目录
    print_message $BLUE "配置Docker服务..."
    mkdir -p /etc/systemd/system/docker.service.d
    
    # 重新加载systemd配置
    systemctl daemon-reload
    
    # 启动Docker服务
    print_message $BLUE "启动Docker服务..."
    if systemctl start docker; then
        print_message $GREEN "✅ Docker服务启动成功！"
    else
        print_message $YELLOW "⚠️  Docker服务启动失败，尝试手动启动..."
        # 尝试直接启动docker daemon
        if docker daemon --host=unix:///var/run/docker.sock --host=tcp://0.0.0.0:2376 --selinux-enabled=false --log-level=warn &
        then
            print_message $GREEN "✅ Docker daemon启动成功！"
            sleep 3
        else
            print_message $RED "❌ Docker启动失败！请检查系统配置"
            exit 1
        fi
    fi
    
    # 设置开机自启
    if systemctl enable docker &> /dev/null; then
        print_message $GREEN "✅ Docker开机自启配置成功！"
    else
        print_message $YELLOW "⚠️  Docker开机自启配置失败，但不影响使用"
    fi
    
    # 验证安装
    if check_docker_installed; then
        print_message $GREEN "✅ Docker安装成功！"
        docker --version
        echo
    else
        print_message $RED "❌ Docker安装失败！"
        exit 1
    fi
}

# Docker镜像仓库配置菜单
docker_mirror_menu() {
    while true; do
        print_title
        echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
        echo -e "${WHITE}Docker镜像仓库配置${NC}"
        echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
        echo -e "${CYAN}1.${NC} ${WHITE}阿里云加速器${NC}"
        echo -e "${CYAN}2.${NC} ${WHITE}腾讯云加速器${NC}"
        echo -e "${CYAN}3.${NC} ${WHITE}七牛云加速器${NC}"
        echo -e "${CYAN}4.${NC} ${WHITE}网易云加速器${NC}"
        echo -e "${CYAN}5.${NC} ${WHITE}返回主菜单${NC}"
        echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
        echo
        read -p "请选择操作 (1-5): " choice
        
        case $choice in
            1)
                configure_aliyun_mirror
                ;;
            2)
                configure_tencent_mirror
                ;;
            3)
                configure_qiniu_mirror
                ;;
            4)
                configure_netease_mirror
                ;;
            5)
                return
                ;;
            *)
                print_message $RED "无效选择，请重新输入！"
                ;;
        esac
        
        echo
        read -p "按回车键继续..."
    done
}

# 配置阿里云镜像
configure_aliyun_mirror() {
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${WHITE}阿里云加速器配置${NC}"
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo
    print_message $YELLOW "阿里云加速器需要登陆到后台控制面板查看"
    print_message $BLUE "地址: https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors"
    echo
    
    while true; do
        read -p "请输入您的阿里云加速器地址: " aliyun_mirror
        
        if [ -z "$aliyun_mirror" ]; then
            print_message $RED "❌ 镜像地址不能为空！"
            continue
        fi
        
        # 校验地址格式
        if [[ ! "$aliyun_mirror" =~ ^https?://[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(:[0-9]+)?(/.*)?$ ]]; then
            print_message $RED "❌ 地址格式不正确！请输入有效的HTTP/HTTPS地址"
            print_message $YELLOW "示例格式: https://xxx.mirror.aliyuncs.com"
            continue
        fi
        
        # 检查是否为阿里云镜像地址
        if [[ ! "$aliyun_mirror" =~ aliyuncs\.com|aliyun\.com ]]; then
            print_message $YELLOW "⚠️  警告：该地址可能不是阿里云镜像地址"
            print_message $BLUE "阿里云镜像地址通常包含: aliyuncs.com 或 aliyun.com"
            read -p "是否继续使用此地址？(y/n，默认n): " continue_use
            if [ "$continue_use" != "y" ] && [ "$continue_use" != "Y" ]; then
                continue
            fi
        fi
        
        # 测试地址连通性
        print_message $BLUE "正在测试地址连通性..."
        if curl -s --connect-timeout 5 --max-time 10 "$aliyun_mirror" > /dev/null 2>&1; then
            print_message $GREEN "✅ 地址连通性测试通过！"
            break
        else
            print_message $RED "❌ 地址连通性测试失败！"
            print_message $YELLOW "可能的原因："
            print_message $YELLOW "1. 网络连接问题"
            print_message $YELLOW "2. 地址不正确"
            print_message $YELLOW "3. 服务器暂时不可用"
            read -p "是否继续使用此地址？(y/n，默认n): " continue_use
            if [ "$continue_use" != "y" ] && [ "$continue_use" != "Y" ]; then
                continue
            fi
            break
        fi
    done
    
    # 创建daemon.json文件
    mkdir -p /etc/docker
    cat > /etc/docker/daemon.json << EOF
{
    "registry-mirrors": ["$aliyun_mirror"],
    "live-restore": true
}
EOF
    
    # 重启Docker服务
    systemctl daemon-reload
    systemctl restart docker
    
    print_message $GREEN "✅ 阿里云镜像配置成功！"
    echo
    print_message $BLUE "配置内容："
    cat /etc/docker/daemon.json
    echo
}

# 配置腾讯云镜像
configure_tencent_mirror() {
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${WHITE}腾讯云加速器配置${NC}"
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo
    
    # 创建daemon.json文件
    mkdir -p /etc/docker
    cat > /etc/docker/daemon.json << EOF
{
    "registry-mirrors": ["https://mirror.ccs.tencentyun.com"],
    "live-restore": true
}
EOF
    
    # 重启Docker服务
    systemctl daemon-reload
    systemctl restart docker
    
    print_message $GREEN "✅ 腾讯云镜像配置成功！"
    echo
    print_message $BLUE "配置内容："
    cat /etc/docker/daemon.json
    echo
}

# 配置七牛云镜像
configure_qiniu_mirror() {
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${WHITE}七牛云加速器配置${NC}"
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo
    
    # 创建daemon.json文件
    mkdir -p /etc/docker
    cat > /etc/docker/daemon.json << EOF
{
    "registry-mirrors": ["https://reg-mirror.qiniu.com"],
    "live-restore": true
}
EOF
    
    # 重启Docker服务
    systemctl daemon-reload
    systemctl restart docker
    
    print_message $GREEN "✅ 七牛云镜像配置成功！"
    echo
    print_message $BLUE "配置内容："
    cat /etc/docker/daemon.json
    echo
}

# 配置网易云镜像
configure_netease_mirror() {
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${WHITE}网易云加速器配置${NC}"
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo
    
    # 创建daemon.json文件
    mkdir -p /etc/docker
    cat > /etc/docker/daemon.json << EOF
{
    "registry-mirrors": ["http://hub-mirror.cn.163.com"],
    "live-restore": true
}
EOF
    
    # 重启Docker服务
    systemctl daemon-reload
    systemctl restart docker
    
    print_message $GREEN "✅ 网易云镜像配置成功！"
    echo
    print_message $BLUE "配置内容："
    cat /etc/docker/daemon.json
    echo
}

# Docker运行环境安装菜单
docker_environment_menu() {
    while true; do
        print_title
        echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
        echo -e "${WHITE}Docker运行环境安装${NC}"
        echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
        echo -e "${CYAN}1.${NC} ${WHITE}Docker搭建Jdk1.8${NC}"
        echo -e "${CYAN}2.${NC} ${WHITE}Docker安装Mysql5.7${NC}"
        echo -e "${CYAN}3.${NC} ${WHITE}Docker安装Nginx${NC}"
        echo -e "${CYAN}4.${NC} ${WHITE}Docker安装Tomcat${NC}"
        echo -e "${CYAN}5.${NC} ${WHITE}Docker安装Redis${NC}"
        echo -e "${CYAN}6.${NC} ${WHITE}Docker安装RabbitMQ${NC}"
        echo -e "${CYAN}7.${NC} ${WHITE}Docker安装Kafka${NC}"
        echo -e "${CYAN}8.${NC} ${WHITE}Docker安装MongoDB${NC}"
        echo -e "${CYAN}9.${NC} ${WHITE}返回主菜单${NC}"
        echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
        echo
        read -p "请选择操作 (1-9): " choice
        
        case $choice in
            1)
                install_jdk18
                ;;
            2)
                install_mysql57
                ;;
            3)
                install_nginx
                ;;
            4)
                install_tomcat
                ;;
            5)
                install_redis
                ;;
            6)
                install_rabbitmq
                ;;
            7)
                install_kafka
                ;;
            8)
                install_mongodb
                ;;
            9)
                return
                ;;
            *)
                print_message $RED "无效选择，请重新输入！"
                ;;
        esac
        
        echo
        read -p "按回车键继续..."
    done
}

# 安装JDK1.8
install_jdk18() {
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${WHITE}Docker搭建Jdk1.8${NC}"
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo
    
    # 检查容器是否已存在
    if docker ps -a --format "table {{.Names}}" | grep -q "jdk18-container"; then
        print_message $YELLOW "⚠️  JDK1.8容器已存在！"
        print_message $BLUE "容器状态："
        docker ps -a --filter "name=jdk18-container" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
        echo
        read -p "是否重新安装？(y/n，默认n): " reinstall
        if [ "$reinstall" = "y" ] || [ "$reinstall" = "Y" ]; then
            print_message $BLUE "正在删除旧容器..."
            docker rm -f jdk18-container
        else
            print_message $GREEN "✅ 使用现有JDK1.8容器"
            return
        fi
    fi
    
    print_message $BLUE "正在拉取OpenJDK 1.8镜像..."
    docker pull openjdk:8-jdk
    
    if [ $? -eq 0 ]; then
        print_message $GREEN "✅ JDK1.8镜像拉取成功！"
        print_message $BLUE "运行JDK1.8容器..."
        docker run -d --name jdk18-container -it openjdk:8-jdk
        print_message $GREEN "✅ JDK1.8容器启动成功！"
    else
        print_message $RED "❌ JDK1.8安装失败！"
    fi
}

# 安装MySQL5.7
install_mysql57() {
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${WHITE}Docker安装Mysql5.7${NC}"
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo
    
    # 检查容器是否已存在
    if docker ps -a --format "table {{.Names}}" | grep -q "mysql57-container"; then
        print_message $YELLOW "⚠️  MySQL5.7容器已存在！"
        print_message $BLUE "容器状态："
        docker ps -a --filter "name=mysql57-container" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
        echo
        read -p "是否重新安装？(y/n，默认n): " reinstall
        if [ "$reinstall" = "y" ] || [ "$reinstall" = "Y" ]; then
            print_message $BLUE "正在删除旧容器..."
            docker rm -f mysql57-container
        else
            print_message $GREEN "✅ 使用现有MySQL5.7容器"
            return
        fi
    fi
    
    read -p "请输入MySQL root密码: " mysql_password
    if [ -z "$mysql_password" ]; then
        mysql_password="123456"
        print_message $YELLOW "使用默认密码: 123456"
    fi
    
    print_message $BLUE "正在拉取MySQL 5.7镜像..."
    docker pull mysql:5.7
    
    if [ $? -eq 0 ]; then
        print_message $GREEN "✅ MySQL5.7镜像拉取成功！"
        print_message $BLUE "运行MySQL5.7容器..."
        docker run -d --name mysql57-container \
            -p 3306:3306 \
            -e MYSQL_ROOT_PASSWORD=$mysql_password \
            -e MYSQL_DATABASE=test \
            -v mysql_data:/var/lib/mysql \
            mysql:5.7
        print_message $GREEN "✅ MySQL5.7容器启动成功！"
        print_message $BLUE "端口: 3306"
        print_message $BLUE "用户名: root"
        print_message $BLUE "密码: $mysql_password"
    else
        print_message $RED "❌ MySQL5.7安装失败！"
    fi
}

# 安装Nginx
install_nginx() {
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${WHITE}Docker安装Nginx${NC}"
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo
    
    # 检查容器是否已存在
    if docker ps -a --format "table {{.Names}}" | grep -q "nginx-container"; then
        print_message $YELLOW "⚠️  Nginx容器已存在！"
        print_message $BLUE "容器状态："
        docker ps -a --filter "name=nginx-container" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
        echo
        read -p "是否重新安装？(y/n，默认n): " reinstall
        if [ "$reinstall" = "y" ] || [ "$reinstall" = "Y" ]; then
            print_message $BLUE "正在删除旧容器..."
            docker rm -f nginx-container
        else
            print_message $GREEN "✅ 使用现有Nginx容器"
            return
        fi
    fi
    
    print_message $BLUE "正在拉取Nginx镜像..."
    docker pull nginx:latest
    
    if [ $? -eq 0 ]; then
        print_message $GREEN "✅ Nginx镜像拉取成功！"
        print_message $BLUE "运行Nginx容器..."
        docker run -d --name nginx-container \
            -p 80:80 \
            -p 443:443 \
            -v nginx_html:/usr/share/nginx/html \
            -v nginx_conf:/etc/nginx \
            nginx:latest
        print_message $GREEN "✅ Nginx容器启动成功！"
        print_message $BLUE "端口: 80, 443"
    else
        print_message $RED "❌ Nginx安装失败！"
    fi
}

# 安装Tomcat
install_tomcat() {
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${WHITE}Docker安装Tomcat${NC}"
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo
    
    # 检查容器是否已存在
    if docker ps -a --format "table {{.Names}}" | grep -q "tomcat-container"; then
        print_message $YELLOW "⚠️  Tomcat容器已存在！"
        print_message $BLUE "容器状态："
        docker ps -a --filter "name=tomcat-container" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
        echo
        read -p "是否重新安装？(y/n，默认n): " reinstall
        if [ "$reinstall" = "y" ] || [ "$reinstall" = "Y" ]; then
            print_message $BLUE "正在删除旧容器..."
            docker rm -f tomcat-container
        else
            print_message $GREEN "✅ 使用现有Tomcat容器"
            return
        fi
    fi
    
    print_message $BLUE "正在拉取Tomcat镜像..."
    docker pull tomcat:latest
    
    if [ $? -eq 0 ]; then
        print_message $GREEN "✅ Tomcat镜像拉取成功！"
        print_message $BLUE "运行Tomcat容器..."
        docker run -d --name tomcat-container \
            -p 8080:8080 \
            -v tomcat_webapps:/usr/local/tomcat/webapps \
            tomcat:latest
        print_message $GREEN "✅ Tomcat容器启动成功！"
        print_message $BLUE "端口: 8080"
    else
        print_message $RED "❌ Tomcat安装失败！"
    fi
}

# 安装Redis
install_redis() {
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${WHITE}Docker安装Redis${NC}"
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo
    
    # 检查容器是否已存在
    if docker ps -a --format "table {{.Names}}" | grep -q "redis-container"; then
        print_message $YELLOW "⚠️  Redis容器已存在！"
        print_message $BLUE "容器状态："
        docker ps -a --filter "name=redis-container" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
        echo
        read -p "是否重新安装？(y/n，默认n): " reinstall
        if [ "$reinstall" = "y" ] || [ "$reinstall" = "Y" ]; then
            print_message $BLUE "正在删除旧容器..."
            docker rm -f redis-container
        else
            print_message $GREEN "✅ 使用现有Redis容器"
            return
        fi
    fi
    
    read -p "请输入Redis密码 (可选): " redis_password
    if [ -n "$redis_password" ]; then
        redis_cmd="--requirepass $redis_password"
    else
        redis_cmd=""
    fi
    
    print_message $BLUE "正在拉取Redis镜像..."
    docker pull redis:latest
    
    if [ $? -eq 0 ]; then
        print_message $GREEN "✅ Redis镜像拉取成功！"
        print_message $BLUE "运行Redis容器..."
        docker run -d --name redis-container \
            -p 6379:6379 \
            -v redis_data:/data \
            redis:latest redis-server $redis_cmd
        print_message $GREEN "✅ Redis容器启动成功！"
        print_message $BLUE "端口: 6379"
        if [ -n "$redis_password" ]; then
            print_message $BLUE "密码: $redis_password"
        fi
    else
        print_message $RED "❌ Redis安装失败！"
    fi
}

# 安装RabbitMQ
install_rabbitmq() {
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${WHITE}Docker安装RabbitMQ${NC}"
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo
    
    # 检查容器是否已存在
    if docker ps -a --format "table {{.Names}}" | grep -q "rabbitmq-container"; then
        print_message $YELLOW "⚠️  RabbitMQ容器已存在！"
        print_message $BLUE "容器状态："
        docker ps -a --filter "name=rabbitmq-container" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
        echo
        read -p "是否重新安装？(y/n，默认n): " reinstall
        if [ "$reinstall" = "y" ] || [ "$reinstall" = "Y" ]; then
            print_message $BLUE "正在删除旧容器..."
            docker rm -f rabbitmq-container
        else
            print_message $GREEN "✅ 使用现有RabbitMQ容器"
            return
        fi
    fi
    
    read -p "请输入RabbitMQ用户名 (默认admin): " rabbitmq_user
    if [ -z "$rabbitmq_user" ]; then
        rabbitmq_user="admin"
    fi
    
    read -p "请输入RabbitMQ密码 (默认123456): " rabbitmq_password
    if [ -z "$rabbitmq_password" ]; then
        rabbitmq_password="123456"
    fi
    
    print_message $BLUE "正在拉取RabbitMQ镜像..."
    docker pull rabbitmq:3-management
    
    if [ $? -eq 0 ]; then
        print_message $GREEN "✅ RabbitMQ镜像拉取成功！"
        print_message $BLUE "运行RabbitMQ容器..."
        docker run -d --name rabbitmq-container \
            -p 5672:5672 \
            -p 15672:15672 \
            -e RABBITMQ_DEFAULT_USER=$rabbitmq_user \
            -e RABBITMQ_DEFAULT_PASS=$rabbitmq_password \
            rabbitmq:3-management
        print_message $GREEN "✅ RabbitMQ容器启动成功！"
        print_message $BLUE "端口: 5672 (AMQP), 15672 (管理界面)"
        print_message $BLUE "用户名: $rabbitmq_user"
        print_message $BLUE "密码: $rabbitmq_password"
    else
        print_message $RED "❌ RabbitMQ安装失败！"
    fi
}

# 安装Kafka
install_kafka() {
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${WHITE}Docker安装Kafka${NC}"
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo
    
    # 检查容器是否已存在
    if docker ps -a --format "table {{.Names}}" | grep -q "kafka-container\|zookeeper-container"; then
        print_message $YELLOW "⚠️  Kafka/Zookeeper容器已存在！"
        print_message $BLUE "容器状态："
        docker ps -a --filter "name=kafka-container" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
        docker ps -a --filter "name=zookeeper-container" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
        echo
        read -p "是否重新安装？(y/n，默认n): " reinstall
        if [ "$reinstall" = "y" ] || [ "$reinstall" = "Y" ]; then
            print_message $BLUE "正在删除旧容器..."
            docker rm -f kafka-container zookeeper-container
        else
            print_message $GREEN "✅ 使用现有Kafka/Zookeeper容器"
            return
        fi
    fi
    
    print_message $BLUE "正在拉取Zookeeper镜像..."
    docker pull confluentinc/cp-zookeeper:latest
    
    print_message $BLUE "正在拉取Kafka镜像..."
    docker pull confluentinc/cp-kafka:latest
    
    if [ $? -eq 0 ]; then
        print_message $GREEN "✅ Kafka镜像拉取成功！"
        print_message $BLUE "运行Zookeeper容器..."
        docker run -d --name zookeeper-container \
            -p 2181:2181 \
            -e ZOOKEEPER_CLIENT_PORT=2181 \
            confluentinc/cp-zookeeper:latest
        
        print_message $BLUE "运行Kafka容器..."
        docker run -d --name kafka-container \
            -p 9092:9092 \
            -e KAFKA_ZOOKEEPER_CONNECT=zookeeper-container:2181 \
            -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092 \
            -e KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1 \
            confluentinc/cp-kafka:latest
        
        print_message $GREEN "✅ Kafka容器启动成功！"
        print_message $BLUE "端口: 9092 (Kafka), 2181 (Zookeeper)"
    else
        print_message $RED "❌ Kafka安装失败！"
    fi
}

# 安装MongoDB
install_mongodb() {
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${WHITE}Docker安装MongoDB${NC}"
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo
    
    # 检查容器是否已存在
    if docker ps -a --format "table {{.Names}}" | grep -q "mongodb-container"; then
        print_message $YELLOW "⚠️  MongoDB容器已存在！"
        print_message $BLUE "容器状态："
        docker ps -a --filter "name=mongodb-container" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
        echo
        read -p "是否重新安装？(y/n，默认n): " reinstall
        if [ "$reinstall" = "y" ] || [ "$reinstall" = "Y" ]; then
            print_message $BLUE "正在删除旧容器..."
            docker rm -f mongodb-container
        else
            print_message $GREEN "✅ 使用现有MongoDB容器"
            return
        fi
    fi
    
    read -p "请输入MongoDB用户名 (默认admin): " mongodb_user
    if [ -z "$mongodb_user" ]; then
        mongodb_user="admin"
    fi
    
    read -p "请输入MongoDB密码 (默认123456): " mongodb_password
    if [ -z "$mongodb_password" ]; then
        mongodb_password="123456"
    fi
    
    print_message $BLUE "正在拉取MongoDB镜像..."
    docker pull mongo:latest
    
    if [ $? -eq 0 ]; then
        print_message $GREEN "✅ MongoDB镜像拉取成功！"
        print_message $BLUE "运行MongoDB容器..."
        docker run -d --name mongodb-container \
            -p 27017:27017 \
            -e MONGO_INITDB_ROOT_USERNAME=$mongodb_user \
            -e MONGO_INITDB_ROOT_PASSWORD=$mongodb_password \
            -v mongodb_data:/data/db \
            mongo:latest
        print_message $GREEN "✅ MongoDB容器启动成功！"
        print_message $BLUE "端口: 27017"
        print_message $BLUE "用户名: $mongodb_user"
        print_message $BLUE "密码: $mongodb_password"
    else
        print_message $RED "❌ MongoDB安装失败！"
    fi
}

# 主菜单
main_menu() {
    while true; do
        print_title
        echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
        echo -e "${WHITE}Docker管理面板${NC}"
        echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
        echo -e "${CYAN}1.${NC} ${WHITE}Docker更换镜像仓库${NC}"
        echo -e "${CYAN}2.${NC} ${WHITE}Docker安装运行环境${NC}"
        echo -e "${CYAN}3.${NC} ${WHITE}退出${NC}"
        echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
        echo
        read -p "请选择操作 (1-3): " choice
        
        case $choice in
            1)
                docker_mirror_menu
                ;;
            2)
                docker_environment_menu
                ;;
            3)
                print_message $GREEN "感谢使用Docker管理面板！"
                exit 0
                ;;
            *)
                print_message $RED "无效选择，请重新输入！"
                ;;
        esac
        
        echo
        read -p "按回车键继续..."
    done
}

# 主函数
main() {
    print_title
    
    # 检查Docker是否已安装
    if check_docker_installed; then
        print_message $GREEN "✅ Docker已安装"
        docker --version
        echo
        main_menu
    else
        print_message $YELLOW "⚠️  Docker未安装"
        echo
        print_message $CYAN "是否安装Docker最新版本?"
        print_message $YELLOW "注意: 回车默认取消，终止运行脚本"
        read -p "输入y安装Docker，回车取消: " install_choice
        
        if [ "$install_choice" = "y" ] || [ "$install_choice" = "Y" ]; then
            install_docker
            echo
            print_message $GREEN "Docker安装完成，进入管理面板..."
            sleep 2
            main_menu
        else
            print_message $YELLOW "取消安装，脚本终止"
            exit 0
        fi
    fi
}

# 检查是否为root用户
if [ "$EUID" -ne 0 ]; then
    print_message $RED "❌ 请使用root用户运行此脚本！"
    exit 1
fi

# 运行主函数
main
